<?php
namespace Festivalcn\Controller;
use Think\Controller;

class IndexController extends BaseController {


	//首页
	public function main(){ 
		$sect = D(C('TABLE_HOLIDAY'));
		$result = $sect->field('fstvbanner')->select();//轮播图
		$selatc = D(C('TABLE_ARTICLE'));
		$where['state'] = 0;//状态0代表显示，1代表隐藏。
		$res = $selatc->field('id,fstvid,title,content,picture,pubtime,urladdr')->where($where)->order('votes desc')->limit(0,6)->select();//最热作品
		$this->assign('lunbo',$result);
		$this->assign('artic',$res);

		/*$fstv = $selatc->field('id,fstvid,title,content,picture,pubtime,urladdr')->order('votes desc')->limit(6,14)->select();//传统节日
		$this->assign('fstv',$fstv);**/
		$resfstv = $sect->field('fstvid,fstvnm,fstvpicture')->order('fstvsort desc')->select();//传统节日
		$this->assign('resfstv',$resfstv);

		$peole = D(C('TABLE_PEOPLE'));//评选专家
		$people = $peole->field('userid,username,job,peopicture,introduce')->select();
		$peonum = $peole->count();
		if($peonum<=10){ 
			$this->assign('renwutupian',$people);
			$this->assign('flag',1);
		}else{
			$peobefore = $peole->field('userid,username,job,peopicture,introduce')->order('userid asc')->limit(0,10)->select();//前10位专家
			$this->assign('renwubefore',$peobefore);
			$peoafter = $peole->field('userid,username,job,peopicture,introduce')->order('userid desc')->limit(0,10)->select();//后10位专家
			$this->assign('peoafter',$peoafter);
		}
//		$this->display();
	}
	//最热作品超链接展示
	public function zuopindetails(){ 
		$atcid = I('get.atcid');
		if(!empty($atcid)){
    		$m =  D(C('TABLE_ARTICLE'));
    		$where['state'] = 0;//状态0代表显示，1代表隐藏。
    		$result = $m->where($where)->find($atcid);

    		//根据ID获取某个节日以及分类的排序
    	    $pm = $this->typesort($result['fstvid'],$result['netid'],$atcid);

    		//dump($result);
    		$datenm =  D(C('TABLE_HOLIDAY'));
    		$res = $datenm->where(array('fstvid'=>$result['fstvid']))->select();
    		//人气作品
    		$map['id']  = array('not in',$atcid);
    		$map['state'] = 0;//状态0代表显示，1代表隐藏。
    		$renqi = $m->where($map)->order('votes desc')->limit(0,4)->select();
    		//人气统计(根据节日来统计文章数量)
    		$where['fstvid'] = $result['fstvid'];
    		$covotes = $m->field('sum(votes) as votestotal')->where($where)->find();
    		$this->assign('detail',$result);
			$this->assign('fstvnm',$res[0]);
			$this->assign('renqi',$renqi);
			$this->assign('pm',$pm);
			$this->assign('cuntvote',$covotes);
    	 }

    	
		if(parent::is_mobile()){ 
			$this->detalatc($atcid);
		}else{ 
			$this->display();	
		}

	}
	//根据数组key排名
	protected function typesort($fstvid,$netid,$actid){ 
		$m =  D(C('TABLE_ARTICLE'));
		$res = $m->where(array('fstvid'=>$fstvid,'netid'=>$netid,'state'=>0))->order('votes desc')->select();
		if($res){ 
			foreach ($res as $key => $value) {
				if($value['id']==$actid){ 
					return $key+1;
				}
			}
		}
	}

	//专家详情页面
	public function pingshen(){ 
		$userid = I('get.userid');
		if(!empty($userid)){
    		$pdtal =  D(C('TABLE_PEOPLE'));
    		$res = $pdtal->where(array('userid'=>$userid))->select();
			$this->assign('userdetal',$res[0]);
    	 }
    	//专家
    	$whe['userid']  = array('not in',$userid);
    	$pingshenren = $pdtal->where($whe)->field('userid,username,job,peopicture,introduce')->order('userid desc')->select();//专家图片
		$this->assign('pingshenren',$pingshenren);
		$this->display();
	}

	//投票列表页面
	public function votelist(){ 
		$fstvid = I('get.fstvid');
		$netid = I('get.netid');
	//	dump($fstvid);
		$fstvnm =  D(C('TABLE_HOLIDAY'));
		$nettype = D(C('TABLE_NETTYPE'));
		$atcObj =  D(C('TABLE_ARTICLE'));

		$holiday= $fstvnm->order('fstvsort desc')->select();//节日分类
		$leixing= $nettype->order('netid ')->select();//网络类型
		$picture= $fstvnm->where(array('fstvid'=>$fstvid))->find();

		if(!empty($fstvid)){ 
			 if(!empty($netid)){
			  	$where['fstvid'] = $fstvid;
			    $where['netid'] = $netid;
			    $where['state'] = 0;//状态0代表显示，1代表隐藏。
			  }else{ 
			  	$where['fstvid'] = $fstvid;
			  	$where['state'] = 0;
			  }		  
			  $article = $atcObj->field('id,votes,title,urladdr,content,authornm,picture,summary')->where($where)->order('votes DESC')->select();
		}else{ 
		  $where['fstvid'] = 7;
		  $where['state'] = 0;
		  if(!empty($netid)){ 
		  $where['netid'] = $netid;
		//$where['state'] = 0;
		  }
		  $article = $atcObj->field('id,votes,title,urladdr,content,authornm,picture,summary')->where($where)->order('votes DESC')->select();
		  
		}
		$this->assign('fstvpic',$picture);
		$this->assign('fstvname',$holiday);
		$this->assign('netobj',$leixing);
		$this->assign('votelist',$article);
		if(parent::is_mobile()){ 
			$this->listvote();
	//		$this->display('mobile/list');
		}else{ 
			$this->display('votelist');
		}
		

	}

	//添加投票数
	public function addVote(){
		$time1='2017-01-04 17:00:00';
	 	$time2= date('Y-m-d H:i:s');
	if(strtotime($time2)<strtotime($time1)){  
		$wip = getClientIP();
		$cdate = date('Y-m-d',time());

		$resvotes = D(C('TABLE_VOTES'));
        $valcount = explode(",",$wip);
        //多个ip地址判断
		if(count($valcount)>2) { 
			$wips = $valcount[1];//获取第2个ip地址
		}else{ 
			$wips = $valcount[0];//获取当前ip地址
		}
	//	$ipcount = $resvotes->field('ip,sum(votenum) as votetotal')->where(array('ip'=>$wip,'createdate'=>$cdate))->find();
		$ipcount = $resvotes->where(array('ip'=>array('like',"%".$wips."%"),'createdate'=>$cdate))->count();
	//	echo $resvotes->getLastSql();die;

		if($ipcount>=3){
			$count = 1;//如果投票次数大于3次，设置标示$count=1;在页面做判断用.
		 }else{
		 	$res = D(C('TABLE_ARTICLE'));
			$where["id"] = $_POST['articid'];
			$result = $res->where($where)->setInc('votes');
			//echo $res->getLastSql();
			if(false!==$result){
			  $val['ip'] = $wip;
			  $val['votenum'] = 1;
			  $val['createdate'] = date('Y-m-d',time());
			  $resvotes->add($val);
			}
			$count = $res->field('votes')->where($where)->find();
		   }
		 }else{ 
		  $count = 5;//设置标示$count=5;在页面做判断用.
		}
		echo json_encode($count);
	}

	//判断是手机端登陆换是PC端登陆
	public function index(){ 
		if(parent::is_mobile()){ 
			$this->display('mobile/index');
		}else{ 
			$this->main();
			$this->fstvnames();	
			$this->display('index');	
		}
	}

	
//获奖作品展示list
public function winList(){
	//节日id
	$fstvid = I('get.fstvid');
	$atclist =  D(C('TABLE_ARTICLE'));
if(!empty($fstvid)){
	 $where["fstvid"] = $fstvid;	
    if($fstvid == 1 ){
	   $where["id"] = array('in','151,159,153,150,154,161,162,160,163,172');
	}elseif ($fstvid == 2) {
	   $where["id"] = array('in','135,82,137,171,185,169,178,177,157,195');
	}elseif ($fstvid == 3) {
	   $where["id"] = array('in','242,265,254,272,270,275,282,299,273,250');
	}elseif ($fstvid == 4) {
	   $where["id"] = array('in','240,243,246,238,239,244,253,251,252,241');
	}elseif ($fstvid == 5) {
	   $where["id"] = array('in','276,280,294,290,248,283,286,278,298,287');
	}elseif ($fstvid == 6) {
	   $where["id"] = array('in','211,214,215,216,220,222,223,224,225,226');
	}elseif ($fstvid == 7) {
	   $where["id"] = array('in','47,84,133,180,179,181,5,184,186,187,188');
	}
	$result = $atclist->where($where)->select();
	echo W('Data/getWinAtc',array('res'=>$result)); 
  }	
  $this->fstvnames();
}

	//获奖获奖专题展示list
 public function winsubject(){
    //节日id
	$fstvid = I('get.fstvid');
	$atclist =  D(C('TABLE_ARTICLE'));
	if(!empty($fstvid)){
	 $where["fstvid"] = $fstvid;	
    if($fstvid == 1 ){
	   $where["id"] = array('in','311,145,148');
	}elseif ($fstvid == 2) {
	   $where["id"] = array('in','142,138,141');
	}elseif ($fstvid == 3) {
	   $where["id"] = array('in','209,201,237');
	}elseif ($fstvid == 4) {
	   $where["id"] = array('in','267,269,264');
	}elseif ($fstvid == 5) {
	   $where["id"] = array('in','308,305,306');
	}elseif ($fstvid == 6) {
	   $where["id"] = array('in','232,234,235');
	}elseif ($fstvid == 7) {
	   $where["id"] = array('in','202,204,207');
	}
	$result = $atclist->where($where)->select();
	echo W('Data/getWinAtc',array('res'=>$result)); 
  }	
   $this->fstvnames();
 }

	//查询节日名称
	public function fstvnames(){
	 $fstvnm =  D(C('TABLE_HOLIDAY'));
	 $holiday= $fstvnm->order('fstvsort desc')->select();//节日分类
	 $this->assign('fstvname',$holiday);
	}

	//节日类别
	public function leibie(){
		$holidaynm =  D(C('TABLE_HOLIDAY'));
		$result = $holidaynm->alias('h')
		->join('fvcn_article as a on a.fstvid = h.fstvid')
		->field('h.fstvid,fstvnm,fstvpicture,sum(votes) as votestotal, count(title) as titles')
		->where('a.state=0')->order('fstvsort DESC')->group('fstvnm')->select();
		$this->assign('leibieList',$result);
		$this->display('mobile/leibie');
	}

	//手机版投票list
	public function listvote(){ 
		//节日id
		$fstvid = I('get.fstvid');
		//网络类型id
		$netid = I('get.netid');
		//网络类型
		$netlist =  D(C('TABLE_NETTYPE'));
		$res = $netlist->order('netid')->select();
		//文章内容
		if(!empty($netid)){
			  	$where['fstvid'] = $fstvid;
			    $where['netid'] = $netid;
			    $where['state'] = 0;//状态0代表显示，1代表隐藏。
			  }else{ 
			  	$where['fstvid'] = $fstvid;
			  	$where['netid'] = 1;
			  	$where['state'] = 0;//状态0代表显示，1代表隐藏。
			  }		
		$artce =  D(C('TABLE_ARTICLE'));
		$req = $artce->where($where)->order('votes desc')->select();
		//echo $artce->getLastSql();
		$this->assign('atclst',$req);
		$this->assign('netlst',$res);
		if(parent::is_mobile()){ 
			$this->display('mobile/list');
		}else{ 
			$this->votelist();	
		}


	}

	//手机版投票list
	public function listajax(){ 
		//节日id
		$fstvid = I('post.fstvid');
		//网络类型id
		$netid = I('post.netid');
		//网络类型
		$netlist =  D(C('TABLE_NETTYPE'));
		$res = $netlist->order('netid ')->select();
		//文章内容
		if(!empty($netid)){
			  	$where['fstvid'] = $fstvid;
			    $where['netid'] = $netid;
			    $where['state'] = 0;//状态0代表显示，1代表隐藏。
			  }else{ 
			  	$where['fstvid'] = $fstvid;
			  	$where['netid'] = 1;
			  	$where['state'] = 0;//状态0代表显示，1代表隐藏。
		  }
		$artce =  D(C('TABLE_ARTICLE'));
		$req = $artce->where($where)->order('votes desc')->select();
		$this->assign('atclst',$req);
		$this->assign('netlst',$res);
		if(parent::is_mobile()){
			echo W('Data/getalllistinfo',array('res'=>$req));
		}else{ 
			$this->votelist();	
		}

	}




	
	//手机版作品详情 参数:文章id
	public function detalatc($atcid){ 
		//文章id
		$atcmy =  D(C('TABLE_ARTICLE'));
		$where['id'] = $atcid;
		$where['state'] = 0;//状态0代表显示，1代表隐藏。
		$rest = $atcmy->where($where)->find();
		//根据ID获取某个节日以及分类的排序
    	$pm = $this->typesort($rest['fstvid'],$rest['netid'],$atcid);

		$match = array();
		$content = $rest['summary'];//视频
		preg_match('/<source[^>]*\s+src="([^"]*)"[^>]*>/is',$content,$match);
		$this->assign('movie',$match[1]);
		$this->assign('pm',$pm);
		$this->assign('atcdetail',$rest);
		$this->display('mobile/details');
	}

 
  //在线咨询
   public function askdetail(){
     	//用户名
		$askuser = I('post.askname');
		//用户名电话
		$asktel = I('post.askphone');
		//内容信息
		$askcontent = I('post.askcontent');
		//咨询方式
		$data["askname"]    = $askuser;
		$data["askphone"]   = $asktel;
		$data["askcontent"] = $askcontent;
		$data["createtime"] = date('Y-m-d H:i:s',time());

		$consult = D(C('TABLE_CONSULTS'));
		$rt = $consult->add($data);
		echo json_encode(1);
   }

   //生成二维码
   public function sccode(){
		$data = I('post.data');
		$actid = I('post.actid');
		echo json_encode(array('data'=>$this->qrcode($actid,$data)));
   }

   //搜索标题和人名
   public function searchAll(){

	if($_POST['keyword']=="在这里搜索..."){
	  $this->error('请输入搜索关键词','',1);
	}else{ 
	  $titlist['title'] = array('like',"%{$_POST['keyword']}%");
	  $titlist['state'] = 0;//状态0代表显示，1代表隐藏。
	  $peoplist['username'] = array('like',"%{$_POST['keyword']}%");
	}
	
   	 $atclist =  D(C('TABLE_ARTICLE'));//查询文章
   	 $atc_list = $atclist->where($titlist)->select();//查询文章详情
   	 $fstvnm  =  D(C('TABLE_HOLIDAY')); //节日名称
   	 $where['fstvid'] = $atc_list[0]["fstvid"]; //根据文章名称查询出节日id
	 $nettype =  D(C('TABLE_NETTYPE'));//节日类型
	 $serche['netid'] = $atc_list[0]["netid"]; //根据文章名称查询出节日类型id
	 $holiday= $fstvnm->order('fstvsort desc')->select();//节日分类
	 $leixing= $nettype->order('netid desc')->select();//网络类型
	 $curholiday= $fstvnm->order('fstvsort desc')->where($where)->find();//当前节日分类
	 $curleixing= $nettype->order('netid desc')->where($serche)->find();//当前网络类型
   	

   	$peolist =  D(C('TABLE_PEOPLE'));//查询专家
   	 $peo_list = $peolist->where($peoplist)->find();


   	 if(!empty($atc_list)){ 
   	 	$this->assign('votelist',$atc_list);
   	 	$this->assign('fstvname',$holiday);
		$this->assign('netobj',$leixing);
		$this->assign('curfstvname',$curholiday);
		$this->assign('curnetobj',$curleixing);
		$this->display("searchAll");
   	 }

   
   	 if(!empty($peo_list)){ 
    	$res = $peolist->where(array('userid'=>$peo_list['userid']))->select();
		$this->assign('userdetal',$res[0]);
    	$pingshenren = $peolist->field('userid,username,job,peopicture,introduce')->order('userid desc')->select();//专家图片
		$this->assign('pingshenren',$pingshenren);
		$this->display("pingshen");
   	 }
   	 
   	if($_POST['keyword']!="在这里搜索..."){ 
   	 	if(empty($atc_list) && empty($peo_list)){ 
   	  	 $this->error('暂无相关信息','',1);
   	    }
   	 }

   }

   
}